﻿CREATE PROCEDURE [dbo].[sp_Get_PagingLeads]
	@PageIndex int,
	@PageSize int
AS
	if @PageIndex = 1
	begin
		select top (@PageSize) ol.RawData from dbo.OrderedLeads ol order by ol.LeadID
	end
	else
	begin
		select top (@PageSize) ol.RawData from dbo.OrderedLeads ol
		where ol.LeadID > (select max(LeadID) 
		from (select top (@PageSize * (@PageIndex - 1)) o.LeadID from dbo.OrderedLeads o
		order by o.LeadID) t) order by ol.LeadID
	end